function UserBcjkh() {
    this.form = this.InitForm();

    var year = $("#hidyear").val();
    var quarter = $("#hidquarter").val();
    $("#year").val(year);
    $("#quarter").val(quarter);

    var month = 3;//根据季度来设置月份
    if (quarter == 2) month = 6;
    if (quarter == 3) month = 9;
    if (quarter == 4) month = 12;
    $("#hidmonth").val(month);

    this.form.render('select');
    $("#bottom_btns").hide();

    this.orgsuytable = this.InitOrgSuyTable();
    this.tab = this.InitTab();
    this.table = null;
}

UserBcjkh.prototype.InitForm = function () {
    var form;
    layui.use('form', function () {
        form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        // 选择框事件，去掉
        form.on('select(year)', function (data) {
            $("#hidyear").val(data.value);
            form.render('select');
            vDSOPay.searchtableorgsuy();
        });
        form.on('select(quarter)', function (data) {
            $("#hidquarter").val(data.value);
            form.render('select');
            vDSOPay.searchtableorgsuy();
        });

        form.render();
    });
    return form;
}

//tab切换
UserBcjkh.prototype.InitTab = function () {
    var element;
    layui.use('element', function () {
        element = layui.element;

        element.on('tab(tabcontent)', function (data) {
            if (data.index == 1) {
                $("#bottom_btns").show();
                if ($("#hidstatus").val() != '') {
                    var status = Number($("#hidstatus").val());
                    if (status > 0) {
                        $("#btnbmtj").attr("disabled", "disabled");
                        $("#btnbmxg").attr("disabled", "disabled");
                        $("#btnbmtj").html("该部门已确认");
                    }
                }
            } else {
                $("#hidorgid").val(0);
                $("#hidorgsummaryid").val(0);
                $("#hidstatus").val('');
                $("#bottom_btns").hide();
                $("#btnbmtj").removeAttr("disabled");
                $("#btnbmxg").removeAttr("disabled");
                $("#realname").val("");
            }
        });

    });
    return element;
}

//组织机构树初始化
UserBcjkh.prototype.InitOrgSuyTable = function () {
    var _this = this;
    window.liger.remove("orgsuylist");
    if (_this.grid == null) {
        _this.grid = $("#orgsuylist").ligerGrid({
            checkbox: true,
            columns: [

                //{ display: '序号', width: 80, name: 'rowNum' },
                {display: '机构名称', width: 250, align: 'left', name: 'orgname', id: "orgname"},
                {display: '年', width: 100, name: 'year'},
                {
                    display: '季度', width: 100, name: 'quarter', render: function (rowdata, rowindex, value) {
                        var month = rowdata.month;
                        var quarter = rowdata.quarter;
                        var str = '';
                        switch (quarter) {
                            case null:
                            case '':
                            case undefined:
                                if (month == "1" || month == "2" || month == "3") {
                                    str = "第一季度";
                                }
                                if (month == "4" || month == "5" || month == "6") {
                                    str = "第二季度";
                                }
                                if (month == 7 || month == 8 || month == 9) {
                                    str = "第三季度";
                                }
                                if (month == 10 || month == 11 || month == 12) {
                                    str = "第四季度";
                                }
                                break;
                            case 1:
                                str = "第一季度";
                                break;
                            case 2:
                                str = "第二季度";
                                break;
                            case 3:
                                str = "第三季度";
                                break;
                            case 4:
                                str = "第四季度";
                                break;
                        }
                        return str;
                    }
                },
                {
                    display: '状态', width: 120, name: 'status', render: function (rowdata, rowindex, value) {
                        var status = rowdata.status;
                        if (!rowdata.usercount || rowdata.usercount <= 1) {
                            status = '9';
                        }
                        var str = '';
                        switch (status) {
                            case null:
                            case '':
                            case undefined:
                                str = "未填报";
                                break;
                            case 0:
                                str = '<span style="color:#B8E986">填报<span>';
                                break;
                            case 1:
                                str = "提交";
                                break;
                            case 2:
                                str = "审核通过";
                                break;
                            case 3:
                                str = "归档";
                                break;
                            case 4:
                                str = "驳回";
                                break;
                            case 5:
                                str = "驳回";
                                break;
                            case  '9':
                                str = '';
                                break;
                        }
                        return str;
                    }
                },
                {
                    display: '操作', width: 100, name: 'userid', render: function (rowdata, rowindex, value) {
                        var html = '';
                        if (rowdata.status != null && typeof(rowdata.status) != 'undefined') {
                            html += ' <img class="btn-img" onclick="vDSOPay.SkipDetail(' + rowdata.orgid + ',' + rowdata.orgsummaryid + ',\'' + rowdata.orgname + '\',' + rowdata.year + ',' + rowdata.month + ')" src="' + ctxStatic + '/images/button/detail.png" lay-event="detail" alt="详情" title="详情">';
                        }
                        if (rowdata.status == 2) {
                            html += '<img class="btn-img" onclick="vDSOPay.updatestatus(' + rowdata.orgsummaryid + ',3)" src="' + ctxStatic + '/images/button/guidang.png" lay-event="guidang" alt="归档" title="归档">';
                            html += '<img class="btn-img" onclick="vDSOPay.updatestatus(' + rowdata.orgsummaryid + ',4)" src="' + ctxStatic + '/images/button/bohui.png" lay-event="bohui" alt="驳回" title="驳回">';
                        }
                        if (rowdata.status != null && typeof(rowdata.status) != 'undefined') {
                            if ($("#hidAdmin").val() == "true") {
                                html += '<img class="btn-img" onclick="vDSOPay.updatestatus(' + rowdata.orgsummaryid + ',4)" src="' + ctxStatic + '/images/button/bohui.png" lay-event="bohui" alt="驳回" title="驳回">';
                            }
                        }
                        return html;
                    }
                }
            ],
            pageSize: 30,
            url: "get_org_work_list" + '?time=' + new Date().getTime(),
            delayLoad: false,            //初始化时是否加载,为true时,不加载,为false时,则加载
            sortName: null,
            dataAction: 'server',
            parms: {
                year: $("#hidyear").val(),
                month: $("#hidmonth").val(),
                orgid: $("#hidorgid").val()
            },
            sortOrder: null,
            sourceObj: _this.grid,
            root: 'items',                       //数据源字段名
            record: 'countRecord',                    //数据源记录数字段名
            pageParmName: 'CurrentPage',               //页索引参数名，(提交给服务器)
            pagesizeParmName: 'PageSize',        //页记录数参数名，(提交给服务器)
            data: {},
            width: "100%",
            height: '100%',
            heightDiff: 0,
            rowHeight: 28,
            onBeforeCheckRow: function (checked, data, rowid, rowdata) {

            },
            onBeforeCheckAllRow: function (checked, element) {


            },
            onSuccess: function (rowdata, rowindex) {
                if (rowdata.status > 0) {
                    alert(rowdata.msg)
                }
            },
            alternatingRow: true,
            border: 0,
            usePager: false,
            tree: {
                columnId: "orgname",
                idField: "orgid",
                parentIDField: "orgparentid"

            }
        })
    }
}

UserBcjkh.prototype.OpTab = function (tabindex, obj) {
    this.tab.tabChange('tabcontent', tabindex);
}

UserBcjkh.prototype.searchtableorgsuy = function () {

    var month = 3;//根据季度来设置月份
    var quarter = $("#hidquarter").val();
    if (quarter == 2) month = 6;
    if (quarter == 3) month = 9;
    if (quarter == 4) month = 12;
    $("#hidmonth").val(month);

    this.grid.setOptions({
        newPage: 1, parms: {
            Method: "",
            year: $("#hidyear").val(),
            month: $("#hidmonth").val(),
            status: $("#hidstatus").val(),
            realname:$("#realname").val(),
            orgid: $("#hidorgid").val()
        }
    });

    this.grid.loadData();
}

UserBcjkh.prototype.InitTableByBt = function () {

    var table;
    var _this = this;
    layui.use('table', function () {
        // var quarter = $('input[name="quarter"]:checked ').val();
        var quarter = $('#quarter').val();
        var month1 = "一月";
        var month2 = "二月";
        var month3 = "三月";
        if (quarter == 2) {
            month1 = "四月";
            month2 = "五月";
            month3 = "六月"
        }
        if (quarter == 3) {
            month1 = "七月";
            month2 = "八月";
            month3 = "九月"
        }
        if (quarter == 4) {
            month1 = "十月";
            month2 = "十一月";
            month3 = "十二月"
        }

        table = layui.table;
        table.render({
            elem: '#list'
            , height: $(window).height() - 240
            , url: 'get_deptsw_dept_list' // 数据接口
            , page: true // 开启分页
            , limit: 40
            , id: 'vtdpuserlist'
            , limits: [20, 40, 80, 100]
            , method: "post"
            , cols: [[ // 表头
                {field: 'realname', title: '姓名', width: 120, align: 'center', rowspan: 3, fixed: 'left'}
                , {field: 'orgname', title: '部门', width: 120, align: 'center', rowspan: 3}
                , {field: 'cardno', title: '身份证号', width: 180, align: 'center', rowspan: 3}
                , {field: 'status', title: '状态', width: 100, align: 'center', rowspan: 3}
                , {field: 'summoney', title: '合计', width: 180, align: 'center', rowspan: 3}
                , {title: '执勤岗位津贴', align: 'center', colspan: 13}
                , {title: '季度保留津贴', align: 'center', colspan: 4}
                , {title: '加班补贴', align: 'center', colspan: 4}
                , {title: '本人签字', width: 100, align: 'center', rowspan: 3}
                , {field: 'remark', title: '备注', width: 180, align: 'center', rowspan: 3, edit: 'text'}
            ],
                [
                    {field: 'dutynumsum', title: '值勤岗位津贴合计', width: 180, align: 'center', rowspan: 2}
                    , {title: '一类岗执行50元标准', width: 100, align: 'center', colspan: 6}
                    , {title: '二类岗执行40元标准', width: 100, align: 'center', colspan: 6}
                    , {field: 'retainnumsum', title: '保留津贴合计', width: 180, align: 'center', rowspan: 2}
                    , {
                    field: 'retainnum1',
                    title: '' + month1 + '',
                    width: 180,
                    align: 'center',
                    rowspan: 2
                }
                    , {
                    field: 'retainnum2',
                    title: '' + month2 + '',
                    width: 180,
                    align: 'center',
                    rowspan: 2
                }
                    , {
                    field: 'retainnum3',
                    title: '' + month3 + '',
                    width: 180,
                    align: 'center',
                    rowspan: 2
                }
                    , {field: 'overtimenumsum', title: '加班补贴合计', width: 180, align: 'center', rowspan: 2}
                    , {
                    field: 'overtimenum1',
                    title: '' + month1 + '',
                    width: 180,
                    align: 'center',
                    rowspan: 2
                }
                    , {
                    field: 'overtimenum2',
                    title: '' + month2 + '',
                    width: 180,
                    align: 'center',
                    rowspan: 2
                }
                    , {
                    field: 'overtimenum3',
                    title: '' + month3 + '',
                    width: 180,
                    align: 'center',
                    rowspan: 2
                }
                ],
                [
                    {field: 'dutynumday1', title: month1+'（每月最高30天）', width: 180, align: 'center'}
                    , {field: 'dutynum1', title: '金额', width: 80, align: 'center'}
                    , {field: 'dutynumday12', title: month2+'（每月最高30天）', width: 180, align: 'center'}
                    , {field: 'dutynum12', title: '金额', width: 80, align: 'center'}
                    , {field: 'dutynumday13', title: month3+'（每月最高30天）', width: 180, align: 'center'}
                    , {field: 'dutynum13', title: '金额', width: 80, align: 'center'}
                    , {field: 'dutynumday2', title: month1+'（每月最高26天）', width: 180, align: 'center'}
                    , {field: 'dutynum2', title: '金额', width: 80, align: 'center'}
                    , {field: 'dutynumday22', title: month2+'（每月最高26天）', width: 180, align: 'center'}
                    , {field: 'dutynum22', title: '金额', width: 80, align: 'center'}
                    , {field: 'dutynumday23', title: month3+'（每月最高26天）', width: 180, align: 'center'}
                    , {field: 'dutynum23', title: '金额', width: 80, align: 'center'}
                ]
            ]
            , request: {
                type: 'post'
            }
            , where: {
                year: $("#year").val(),
                quarter: $('#quarter').val(),
                orgid: $('#hidorgid').val(),
                realname:$('#realname').val(),
                status: $("#hidstatus").val()
            }
            , done: function (res, page, count) {
                var message = res.msg;
                $("#table_info").val(message);
                $("[data-field='status']").children().each(function (index) {
                    var text = $(this).text();
                    if (text == "0") {
                        $(this).text('保存')
                    }
                    else if (text == "1") {
                        $(this).text('确认')
                    }
                    else if (text == "2") {
                        $(this).text('审核通过')
                    }
                    else if (text == "3") {
                        $(this).text('归档')
                    }
                    else if (text == "4"||text=="5") {
                        $(this).text('驳回')
                    }
                })
            }
        });
    });
    return table;
}

//表格初始化
UserBcjkh.prototype.SkipDetail = function (orgid, orgsummaryid, orgname, year, month) {
    $("#hidorgid").val(orgid);
    $("#hidorgsummaryid").val(orgsummaryid);
    var quarter = "1";
    if (month == 4 || month == 5 || month == 6) quarter = 2;
    if (month == 7 || month == 8 || month == 9) quarter = 3;
    if (month == 10 || month == 11 || month == 12) quarter = 4;
    $("#hidquarter").val(quarter);
    $("#hidmonth").val(month);
    this.OpTab(2);

    $("#table_title").html("房山公安分局" + orgname + " " + year + " 年第" + quarter + " 季 季度津贴");
    this.table = this.InitTableByBt(orgid, orgsummaryid)
}


//表查询
UserBcjkh.prototype.searchtable = function () {
    var quarter = $('quarter').val();
    var month1 = "一月";
    var month2 = "二月";
    var month3 = "三月";
    if (quarter == 2) {
        month1 = "四月";
        month2 = "五月";
        month3 = "六月"
    }
    if (quarter == 3) {
        month1 = "七月";
        month2 = "八月";
        month3 = "九月"
    }
    if (quarter == 4) {
        month1 = "十月";
        month2 = "十一月";
        month3 = "十二月"
    }
    this.table.reload('vtdpuserlist', {
        cols: [[ // 表头
            {
                field: 'realname',
                title: '姓名',
                width: 120,
                align: 'center',
                rowspan: 3,
                fixed: 'left',
                templet: '#realnameTpl'
            }
            , {field: 'orgname', title: '部门', width: 120, align: 'center', rowspan: 3}
            , {field: 'cardno', title: '身份证号', width: 180, align: 'center', rowspan: 3}
            , {field: 'status', title: '状态', width: 100, align: 'center', rowspan: 3}
            , {field: 'summoney', title: '合计', width: 180, align: 'center', rowspan: 3}
            , {title: '执勤岗位津贴', align: 'center', colspan: 13}
            , {title: '季度保留津贴', align: 'center', colspan: 4}
            , {title: '加班补贴', align: 'center', colspan: 4}
            , {title: '本人签字', width: 100, align: 'center', rowspan: 3}
            , {field: 'remark', title: '备注', width: 180, align: 'center', rowspan: 3, edit: 'text'}
        ],
            [
                {field: 'dutynumsum', title: '值勤岗位津贴合计', width: 180, align: 'center', rowspan: 2}
                , {title: '一类岗执行50元标准', width: 100, align: 'center', colspan: 6}
                , {title: '二类岗执行40元标准', width: 100, align: 'center', colspan: 6}
                , {field: 'retainnumsum', title: '保留津贴合计', width: 180, align: 'center', rowspan: 2}
                , {field: 'retainnum1', title: '' + month1 + '', width: 180, align: 'center', rowspan: 2}
                , {field: 'retainnum2', title: '' + month2 + '', width: 180, align: 'center', rowspan: 2}
                , {field: 'retainnum3', title: '' + month3 + '', width: 180, align: 'center', rowspan: 2}
                , {field: 'overtimenumsum', title: '加班补贴合计', width: 180, align: 'center', rowspan: 2}
                , {
                field: 'overtimenum1',
                title: '' + month1 + '',
                width: 180,
                align: 'center',
                rowspan: 2
            }
                , {
                field: 'overtimenum2',
                title: '' + month2 + '',
                width: 180,
                align: 'center',
                rowspan: 2
            }
                , {
                field: 'overtimenum3',
                title: '' + month3 + '',
                width: 180,
                align: 'center',
                rowspan: 2
            }
            ],
            [
                {field: 'dutynumday1', title: month1+'（每月最高30天）', width: 180, align: 'center'}
                , {field: 'dutynum1', title: '金额', width: 80, align: 'center'}
                , {field: 'dutynumday12', title: month2+'（每月最高30天）', width: 180, align: 'center'}
                , {field: 'dutynum12', title: '金额', width: 80, align: 'center'}
                , {field: 'dutynumday13', title: month3+'（每月最高30天）', width: 180, align: 'center'}
                , {field: 'dutynum13', title: '金额', width: 80, align: 'center'}
                , {field: 'dutynumday2', title: month1+'（每月最高26天）', width: 180, align: 'center'}
                , {field: 'dutynum2', title: '金额', width: 80, align: 'center'}
                , {field: 'dutynumday22', title: month2+'（每月最高26天）', width: 180, align: 'center'}
                , {field: 'dutynum22', title: '金额', width: 80, align: 'center'}
                , {field: 'dutynumday23', title: month3+'（每月最高26天）', width: 180, align: 'center'}
                , {field: 'dutynum23', title: '金额', width: 80, align: 'center'}
            ]
        ]
        , page: {
            curr: 1 //重新从第 1 页开始
        }
        , where: {
            year: $("#year").val(),
            quarter: $('#quarter').val(),
            realname:$('#realname').val(),
            orgid: $("#szdw").val()
        }
    });
}

UserBcjkh.prototype.guidang = function () {
    var _this = this;
    top.mylayui.confirm('确定要全部归档吗？', {icon: 0}, function (index) {
        top.mylayui.close(index);
        var data = _this.grid.getCheckedRows();
        var length = data.length;
        if (length == 0) {
            top.mylayui.msg('请选择部门', {icon: 0});
            return;
        }
        var orgsummaryids = '';
        for (i = 0; i < length; i++) {
            orgsummaryids += data[i].orgsummaryid + ',';
        }

        vDSOPay.updatestatusBm(orgsummaryids, 3);
    });

}

UserBcjkh.prototype.querydetail = function () {
    if ($("#hidorgid").val() == "" || $("#hidorgid").val() == "0") {
        top.mylayui.msg('请先点机构列表后面详情按钮，再点击查询', {icon: 0});
        return;
    }

    this.InitTableByBt($("#hidorgid").val(), $("#hidorgsummaryid").val());
}


// UserBcjkh.prototype.tableref = function () {
//     // var Alldata = this.table.cache;
//     this.table.reload('vtdpuserlist', {
//         where: {
//             year: $("#year").val(),
//             month: $("#month").val(),
//             orgid: $("#szdw").val()
//         }
//     });
// }

UserBcjkh.prototype.exportexcelall = function () {
    top.ShowZheZhao();
    var year = $("#year").val();
    var quarter = $("#quarter").val();
    var orgid = $("#hidorgid").val();
    $.ajax({
        type: "POST",
        url: "deptsw_pay_export",
        data: {year: year, quarter: quarter,orgid: orgid, owner: 0},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                window.open(data.msg, '_self');
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {
            top.ShowZheZhao();
        },
        error: function () {
        }
    });
}

UserBcjkh.prototype.exportall = function () {
    top.ShowZheZhao();
    var year = $("#year").val();
    var quarter = $("#quarter").val();
    var orgid = $("#hidorgid").val();
    $.ajax({
        type: "POST",
        url: "deptsw_export_all",
        data: {year: year, quarter: quarter,orgid: orgid, owner: 0},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                window.open(data.msg, '_self');
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {
            top.ShowZheZhao();
        },
        error: function () {
        }
    });
}


UserBcjkh.prototype.export = function () {
    top.ShowZheZhao();
    var year = $("#year").val();
    var quarter = $("#quarter").val();
    var orgid = $("#hidorgid").val();
    $.ajax({
        type: "POST",
        url: "deptsw_pay_export",
        data: {year: year, quarter: quarter, orgid: orgid, owner: 0},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                window.open(data.msg, '_self');
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {
            top.ShowZheZhao();
        },
        error: function () {
        }
    });
}

UserBcjkh.prototype.addresult = function (result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        top.mylayui.closeAll('iframe');
        vDSOPay.searchtable();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}

UserBcjkh.prototype.updatestatus = function (orgsummaryid, status) {
    top.mylayui.confirm('是否确定该操作？', {icon: 0}, function (index, dd) {
        top.mylayui.close(index);
        $.ajax({
            type: "POST",
            url: "deptsw_pay_update_status",
            data: {orgsummaryid: orgsummaryid, status: status},
            beforeSend: function (XHR) {

            },
            success: function (data) {
                data = eval('(' + data + ')');
                if (data.result > 0) {
                    top.mylayui.msg(data.msg, {icon: 1});
                    vDSOPayDept.searchtable();
                }
                else {
                    top.mylayui.msg(data.msg, {icon: 0});
                }
            },
            complete: function (XHR) {

            },
            error: function () {

            }
        });
    })
}


UserBcjkh.prototype.updatestatusBm = function (orgsummaryids, status) {
    $.ajax({
        type: "POST",
        url: "deptsw_pay_update_statusbm",
        data: {orgsummaryids: orgsummaryids, status: status},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                //vWorksPay.searchtableorgsuy();
                vDSOPay.searchtableorgsuy();
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}

var vDSOPay;
$(function () {
    vDSOPay = new UserBcjkh();
})